using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectLoaiHinhThamDinhGia()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIHINHTHAMDINHGIA
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectLoaiHinhThamDinhGiaPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIHINHTHAMDINHGIA,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLoaiHinhThamDinhGia", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteLoaiHinhThamDinhGia(int id)
		{
			var deleteItem = _repLoaiHinhThamDinhGia.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var result = _repLoaiHinhThamDinhGia.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repLoaiHinhThamDinhGia.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfLoaiHinhThamDinhGia(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_LOAIHINHTHAMDINHGIA,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateLoaiHinhThamDinhGia(FormCollection forms)
		{
			string id = forms.GetValues("LoaiHinhThamDinhGia_ID")[0];
			string tenloaihinhthamdinhgia = forms.GetValues("LoaiHinhThamDinhGia_TenLoaiHinhThamDinhGia")[0];
			string gioithieu = forms.GetValues("LoaiHinhThamDinhGia_GioiThieu")[0];
			string gioithieurutgon = forms.GetValues("LoaiHinhThamDinhGia_GioiThieuRutGon")[0];
			var file = Request.Files["LoaiHinhThamDinhGia_HinhMinhHoa"];
			string refName = "";
			var editItem = _repLoaiHinhThamDinhGia.SelectByID(int.Parse(id));
			editItem.TenLoaiHinhThamDinhGia = tenloaihinhthamdinhgia;
			editItem.GioiThieu = gioithieu;
			editItem.GioiThieuRutGon = gioithieurutgon;
			var result = false;
			if (file.ContentLength != 0)
			{
				result = UploadFile(file, "/Content/Images/Items/", id, ref refName);
				editItem.HinhMinhHoa = refName;
			}
			result = _repLoaiHinhThamDinhGia.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_LOAIHINHTHAMDINHGIA,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfLoaiHinhThamDinhGia", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertLoaiHinhThamDinhGia(FormCollection forms)
		{
			string tenloaihinhthamdinhgia = forms.GetValues("LoaiHinhThamDinhGia_TenLoaiHinhThamDinhGia")[0];
			string gioithieu = forms.GetValues("LoaiHinhThamDinhGia_GioiThieu")[0];
			string gioithieurutgon = forms.GetValues("LoaiHinhThamDinhGia_GioiThieuRutGon")[0];
			var file = Request.Files["LoaiHinhThamDinhGia_HinhMinhHoa"];
			string refName = "";
			var newItem = new LoaiHinhThamDinhGia
			{
				TenLoaiHinhThamDinhGia = tenloaihinhthamdinhgia,
				GioiThieu = gioithieu,
				GioiThieuRutGon = gioithieurutgon,
				HinhMinhHoa = "noimages.jpg"
			};
			var result = _repLoaiHinhThamDinhGia.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
					if (result)
					{
						var addedItem = _repLoaiHinhThamDinhGia.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repLoaiHinhThamDinhGia.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIHINHTHAMDINHGIA,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectLoaiHinhThamDinhGia", CreateViewModel(data));
		}


	}
}